package com.goods.service;

import com.goods.dao.LoginMapper;
import com.goods.pojo.User;
import com.goods.tool.Result;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class LoginService {
    @Autowired
    private  LoginMapper loginMapper;
    @Autowired
    private HttpServletRequest request;
    //登录
    public Result login(User user){
        User login = loginMapper.login(user.getUsername());
        if(login == null){
            return Result.error("账号或密码错误");
        }
        if(!login.getPassword().equals(user.getPassword())){
            return Result.error("账号或密码错误");
        }
        //存入session，
        HttpSession session = request.getSession();
        session.setAttribute("user",user);
        return Result.success();
    }
    //注册
    public Result regist(User user){
        int regist = loginMapper.regist(user);
        if(regist != 1){
            return Result.error("注册失败");
        }
        return Result.success();
    }
}
